#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include<pthread.h>


/*
	线程退出，并回收
*/

int g_val = 9;

void* mythread(void *arg)
{
	int*p = (int*)arg;
	printf("arg=[%d]\n",*p);
	printf("child thread,pid=[%d],id=[%ld]\n",getpid(),pthread_self());
	int a = 0;
	printf("[%p]\n",&g_val);  //必须用全局变量或者是在堆区开辟内存，因为栈上的变量当线程退出后内存将会被回收
	pthread_exit(&g_val);
}


int main()
{
	int num = 99;
	pthread_t thread; //返回线程号	
	int ret = pthread_create(&thread,NULL,mythread,&num);
	if(ret != 0)
	{
		printf("thread creat error,[%s]\n",strerror(ret));
		return -1;
	} 

	printf("parent thread,pid=[%d],id=[%ld]\n",getpid(),pthread_self());

	//目的是为了让子线程能够执行起来
	//sleep(1);
	void *p=NULL;
	pthread_join(thread,&p);
	int n = *(int *)p;
	printf("child exit status:[%d],[%p]\n",n,p);

	return 0;
}
